package bookticket.crm.user.controller;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import bookticket.crm.user.entity.UserEntity;
import bookticket.crm.user.service.UserService;
import bookticket.crm.vip.entity.VipEntity;


@Controller
@RequestMapping("/user")
public class UserController {
	@Autowired
	public UserService service;
	
	@ResponseBody
	@RequestMapping("/register")
	public Map<String,String> register(UserEntity student){
		Map<String,String> map = new HashMap<String,String>();
		int counter = service.register(student);
		if(counter == 1){
			map.put("msg", "增加成功");
			map.put("result","success" );
		}
		else{
			map.put("msg", "增加失败");
			map.put("result","failure" );
		}
		return map;
	}
	
	@ResponseBody
	@RequestMapping("/findAll")
	public Map<String,Object> findAll(Integer page,Integer rows){
		
		Integer before = (page-1)*rows;
		Integer after = rows;
		
		Map<String, Object> pageMap = new HashMap<String,Object>();
		pageMap.put("before", before);
		pageMap.put("after", after);
		
		List<UserEntity> list = service.findAll(pageMap);
		Map<String, Object> map = new HashMap<String,Object>();
		
		//分页查询中固定的写法  total rows
		int total = service.getTotalCounter();
		map.put("total", total);
		map.put("rows", list);
		map.put("result", "success");
		
		return map;
	}
	
	
	@ResponseBody
	@RequestMapping("/findByPhoneAndPassword")
	public UserEntity findByPhoneAndPassword(UserEntity entity){
		return service.findByPhoneAndPassword(entity);
	}
	
	@ResponseBody
	@RequestMapping("/findByPhone")
	public UserEntity findByPhone(HttpServletRequest request){
		String phone = (String) request.getSession().getAttribute("phone");
		return service.findByPhone(phone);
	}
	
	@ResponseBody
	@RequestMapping("/login")
	public Map<String, String> login(UserEntity entity,HttpServletRequest request){
		
		Map<String, String> map = new HashMap<String,String>();
		HttpSession session = request.getSession();
		String phone = entity.getPhone();
		
		UserEntity users = service.findByPhoneAndPassword(entity);
		
		if(users!=null){
			session.setAttribute("phone", phone);
			map.put("msg", "登录成功");
			map.put("result", "success");
		}
		else{
			map.put("msg", "登录失败");
			map.put("result", "failure");
		}
		System.err.println(users);
		return map;
		
	}
	
	
	@ResponseBody
	@RequestMapping("/updateByPhone")
	public Map<String,String> updateByPhone(UserEntity userEntity){
		int counter = service.updateByPhone(userEntity);
		System.err.println("counter:"+counter);
		Map<String,String> map = new HashMap<String,String>();
		if(counter == 1){
			map.put("msg", "修改成功");
			map.put("result","success" );
		}
		else{
			map.put("msg", "修改失败");
			map.put("result","failure" );
		}
		return map;
	}

	
	@ResponseBody
	@RequestMapping("/findUserVipId")
	public VipEntity findUserVipId(UserEntity entity,HttpServletRequest request){
//		String phone = (String)request.getSession().getAttribute("phone");
//		entity = service.findByPhone(phone);
		
		int vipId = 4;
		int totalRecharge = service.findTotalRechargeAmount();
		
//		VipEntity vipEntity = new VipEntity();
		List<VipEntity> vipList = service.findVipAll();
		for (VipEntity vip : vipList) {
			vipId = vip.getId();
			int limit = vip.getRechargeLimit();
			int upper = vip.getRechargeUpper();
			if(totalRecharge>=limit && totalRecharge<=upper){
				entity.setVipId(vipId);
			}
		}
		entity.setVipId(vipId);
		
		return null;
	}
	
	
}
